<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29 0029
 * Time: 下午 5:34
 */
namespace app\wx\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use app\wx\controller\Home;

class Order extends Home
{
    //我的订单
    public function order(){
        $this->is_login();
        $table1s = Db::name('core_table1')->where('register_id',session('user_id'))->select();
        $table2s = Db::name('core_table2')->where('register_id',session('user_id'))->select();
        $table3s = Db::name('core_table3')->where('register_id',session('user_id'))->select();
        $table4s = Db::name('core_table4')->where('register_id',session('user_id'))->select();
        foreach ($table1s as &$row){
            $row['register'] = Db::name('core_register')->where('id',$row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id',$row['pid'])->find();
        }
        foreach ($table2s as &$row){
            $row['register'] = Db::name('core_register')->where('id',$row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id',$row['pid'])->find();
        }

        foreach ($table3s as &$row) {
            $row['register'] = Db::name('core_register')->where('id', $row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id', $row['pid'])->find();
        }

        foreach ($table4s as &$row){
            $row['register'] = Db::name('core_register')->where('id',$row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id',$row['pid'])->find();
        }
        $this->view->table1s = $table1s;
        $this->view->table2s = $table2s;
        $this->view->table3s = $table3s;
        $this->view->table4s = $table4s;
        return $this->fetch();
    }

    //商户订单
    public function sh_order(){
        $this->is_login();
        $table1s = Db::name('core_table1')->where('apply_id',session('user_id'))->select();
        $table2s = Db::name('core_table2')->where('apply_id',session('user_id'))->select();
        $table3s = Db::name('core_table3')->where('apply_id',session('user_id'))->select();
        $table4s = Db::name('core_table4')->where('apply_id',session('user_id'))->select();
        foreach ($table1s as &$row){
            $row['register'] = Db::name('core_register')->where('id',$row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id',$row['pid'])->find();
        }
        foreach ($table2s as &$row){
            $row['register'] = Db::name('core_register')->where('id',$row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id',$row['pid'])->find();
        }

        foreach ($table3s as &$row) {
            $row['register'] = Db::name('core_register')->where('id', $row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id', $row['pid'])->find();
        }

        foreach ($table4s as &$row){
            $row['register'] = Db::name('core_register')->where('id',$row['register_id'])->find();
            $row['manage'] = Db::name('core_form_manage')->where('id',$row['pid'])->find();
        }
        $this->view->table1s = $table1s;
        $this->view->table2s = $table2s;
        $this->view->table3s = $table3s;
        $this->view->table4s = $table4s;
        return $this->fetch();
    }

    //我的订单详情
    public function orderdetail(){
        $this->is_login();
        $table = Db::name('core_table'.input('mid'))->where('id',input('tid'))->find();
        $arr_value = array();
        $arr_text = array();
        unset($table['id']);
        unset($table['pid']);
        unset($table['register_id']);
        unset($table['apply_id']);
        unset($table['order_num']);
        unset($table['status']);
        unset($table['create_time']);
        $i=0;
        $j=0;
        foreach($table as $key=>$v){
            $arr_value[$i] = $v;
            $i++;
        }
        foreach($table as $key=>$v){
            $arr_text[$j] = Db::name('core_form_name_manage')->where('name',$key)->value('text');
            $j++;
        }
        foreach($arr_text as $k => $v){
            foreach($arr_value as $kk => $vv){
                if($k == $kk){
                    $arr[$k][$kk] = $v;
                    $arr[$k][$kk+1] = $vv;
                }
            }
        }
        $this->view->order = $arr;
        $this->view->arr_text = $arr_text;
        $this->view->arr_value = $arr_value;
        $apply_id = Db::name('core_table'.input('mid'))->where('id',input('tid'))->value('apply_id');
        $this->view->sh_name = Db::name('core_register')->where('id',$apply_id)->value('sh_name');
        return $this->fetch();
    }

    //商户订单详情
    public function shorderdetail(){
        $this->is_login();
        $table = Db::name('core_table'.input('mid'))->where('id',input('tid'))->find();
        $arr_value = array();
        $arr_text = array();
        unset($table['id']);
        unset($table['pid']);
        unset($table['register_id']);
        unset($table['apply_id']);
        unset($table['order_num']);
        unset($table['status']);
        unset($table['create_time']);
        $i=0;
        $j=0;
        foreach($table as $key=>$v){
            $arr_value[$i] = $v;
            $i++;
        }
        foreach($table as $key=>$v){
            $arr_text[$j] = Db::name('core_form_name_manage')->where('name',$key)->value('text');
            $j++;
        }
        //一维转二维
//        foreach($arr_text as $k => $v){
//            foreach($arr_value as $kk => $vv){
//                if($k == $kk){
//                    $arr[$k][$kk] = $v;
//                    $arr[$k][$kk+1] = $vv;
//                }
//            }
//        }
//        $this->view->order = $arr;
        $this->view->arr_text = $arr_text;
        $this->view->arr_value = $arr_value;
        $register_id = Db::name('core_table'.input('mid'))->where('id',input('tid'))->value('register_id');
        $this->view->register = Db::name('core_register')->where('id',$register_id)->find();
        $this->view->tid = input('tid');
        $this->view->mid = input('mid');
        $this->view->status = Db::name('core_table'.input('mid'))->where('id',input('tid'))->value('status');
        return $this->fetch();
    }

    //商户确认
    public function confirm(){
        Db::name('core_table'.input('mid'))->where('id',input('tid'))->update(['status'=>2]);
        return json(200);
    }

    //申请退还
    public function apply_return_go(){
        $this->is_login();
        if($this->request->isPost()){
            $data = input('post.');
            $data['register_id'] = session('user_id');
            $data['create_time'] = time();
            unset($data['mid']);
            Db::name('core_return_money')->insert($data);
            Db::name('core_table'.input('mid'))->where('id',input('order_id'))->update(['status'=>3]);
            return json(200);
        }
    }
}